﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyShopThoiTrang.QuanLyShopThoiTrang_BUS_WS;

namespace QuanLyShopThoiTrang
{
    public partial class QuanLyNhanVien : Form
    {
        int iIndex = 0;
        NhanVien temp;
        bool isEditing = false;

        QLSTT_BUSSoapClient ws = new QLSTT_BUSSoapClient();
        public QuanLyNhanVien()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ThemNhanVien tnv = new ThemNhanVien();
            tnv.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<LoaiNhanVien> listLNV = ws.LoaiNhanVien_LayDanhSach();
            comboBox1.DataSource = listLNV;
            comboBox1.DisplayMember = "LoaiNhanVien_Ten";

            NhanVien temp = new NhanVien();
            temp.MaNhanVien = -1;
            List<NhanVien> listNV = ws.NhanVien_TraCuu(temp);

            dataGridView1.Rows.Clear();
            foreach(NhanVien nv in listNV)
            {
                LoaiNhanVien lnv = ws.NhanVien_LayLoaiNV(nv);
                dataGridView1.Rows.Add(dataGridView1.Rows.Count + 1, nv.NhanVien_TenDayDu, lnv.LoaiNhanVien_Ten, nv.NhanVien_Phai, nv.NhanVien_NgaySinh.ToString(), nv.NhanVien_DiaChi, nv.NhanVien_Email, nv.NhanVien_SoDienThoai, nv.NhanVien_Luong, nv.NhanVien_TenDangNhap);
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Tag = nv;
            }
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox9.Text = "";
            textBox10.Text = "";

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex];
            NhanVien nv = (NhanVien)row.Tag;

            try
            {
                Image temp = Image.FromFile(nv.NhanVien_HinhAnh);
                Graphics g = pictureBox2.CreateGraphics();
                g.DrawImage(temp, new Rectangle(0, 0, 110, 110), new Rectangle(0, 0, temp.Width, temp.Height), GraphicsUnit.Pixel);
                g.Dispose();
            }
            catch (System.Exception ex)
            {
                pictureBox2.Image = Properties.Resources.no_image;
            }

            textBox1.Text = nv.NhanVien_Ho;
            textBox2.Text = nv.NhanVien_TenLot;
            textBox3.Text = nv.NhanVien_Ten;
            textBox4.Text = nv.NhanVien_Phai;

            comboBox1.Enabled = true;
            foreach(LoaiNhanVien lnv in comboBox1.Items)
            {
                if(lnv.MaLoaiNhanVien == nv.MaLoaiNhanVien)
                    comboBox1.SelectedItem = lnv;
            }
            comboBox1.Enabled = false;

            textBox6.Text = nv.NhanVien_NgaySinh.ToString();
            textBox7.Text = nv.NhanVien_DiaChi;
            textBox8.Text = nv.NhanVien_SoDienThoai;
            textBox9.Text = nv.NhanVien_Email;
            textBox10.Text = nv.NhanVien_Luong.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (isEditing)
            {
                if (textBox3.Text == "")
                {
                    MessageBox.Show("Không được để trống tên");
                    return;
                }

                if (textBox10.Text == "")
                {
                    MessageBox.Show("Không được để trống lương");
                    return;
                }

                int ma = temp.MaNhanVien;
                string phai = temp.NhanVien_Phai;
                DateTime dt = (DateTime)temp.NhanVien_NgaySinh;
                string user = temp.NhanVien_TenDangNhap;
                string pass = temp.NhanVien_MatKhau;
                string hinhAnh = temp.NhanVien_HinhAnh;

                temp = new NhanVien();

                temp.NhanVien_HinhAnh = hinhAnh;
                temp.NhanVien_TenDangNhap = user;
                temp.NhanVien_MatKhau = pass;
                temp.NhanVien_TrangThai = "Su dung";
                temp.MaNhanVien = ma;
                temp.NhanVien_NgaySinh = dt;
                temp.NhanVien_Phai = phai;

                temp.NhanVien_Ho = textBox1.Text;
                temp.NhanVien_TenLot = textBox2.Text;
                temp.NhanVien_Ten = textBox3.Text;
                temp.NhanVien_TenDayDu = textBox1.Text.Trim() + " " + textBox2.Text.Trim() + " " + textBox3.Text.Trim();

                temp.NhanVien_DiaChi = textBox7.Text;
                temp.NhanVien_Email = textBox9.Text;
                temp.NhanVien_SoDienThoai = textBox8.Text;
                if(ws.KiemTraDouble(textBox10.Text))
                    temp.NhanVien_Luong = double.Parse(textBox10.Text);
                else
                {
                    MessageBox.Show("Lương không hợp lệ");
                    return;
                }

                LoaiNhanVien lsp = (LoaiNhanVien)comboBox1.SelectedItem;
                temp.MaLoaiNhanVien = lsp.MaLoaiNhanVien;

                dataGridView1.Rows[iIndex].Tag = temp;
                dataGridView1.Rows[iIndex].SetValues(dataGridView1.Rows.Count + 1, temp.NhanVien_TenDayDu, lsp.LoaiNhanVien_Ten, temp.NhanVien_Phai, temp.NhanVien_NgaySinh.ToString(), temp.NhanVien_DiaChi, temp.NhanVien_Email, temp.NhanVien_SoDienThoai, temp.NhanVien_Luong, temp.NhanVien_TenDangNhap);
                if (ws.NhanVien_CapNhat(temp))
                {
                    MessageBox.Show("Thành công");
                }
                else
                {
                    MessageBox.Show("Thất bại");
                }

                textBox1.ReadOnly = true;
                textBox2.ReadOnly = true;
                textBox3.ReadOnly = true;
                //textBox4.ReadOnly = true;
                //textBox5.ReadOnly = false;
                //textBox6.ReadOnly = true;
                textBox7.ReadOnly = true;
                textBox8.ReadOnly = true;
                textBox9.ReadOnly = true;
                textBox10.ReadOnly = true;

                dataGridView1.Enabled = true;
                button2.Enabled = true;
                button1.Enabled = true;
                button5.Enabled = true;
                button6.Enabled = true;

                comboBox1.Enabled = false;
                isEditing = false;
            }
            else
            {
                if (dataGridView1.SelectedCells.Count == 0)
                {
                    MessageBox.Show("Hãy chọn một nhân viên để thay đổi");
                    return;
                }
                DataGridViewRow row = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex];

                temp = (NhanVien)row.Tag;
                iIndex = row.Index;

                dataGridView1.Enabled = false;
                button2.Enabled = false;
                button1.Enabled = false;
                button5.Enabled = false;
                button6.Enabled = false;

                textBox1.ReadOnly = false;
                textBox2.ReadOnly = false;
                textBox3.ReadOnly = false;
                //textBox4.ReadOnly = false;
                //textBox6.ReadOnly = false;
                textBox7.ReadOnly = false;
                textBox8.ReadOnly = false;
                textBox9.ReadOnly = false;
                textBox10.ReadOnly = false;

                comboBox1.Enabled = true;

                isEditing = true;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            TraCuuNhanVien tcnv = new TraCuuNhanVien();
            tcnv.Set(this);
            tcnv.ShowDialog();
        }

        public void TraCuu(NhanVien nvx)
        {
            List<NhanVien> listNV = ws.NhanVien_TraCuu(nvx);
            dataGridView1.Rows.Clear();
            foreach (NhanVien nv in listNV)
            {
                LoaiNhanVien lnv = ws.NhanVien_LayLoaiNV(nv);
                dataGridView1.Rows.Add(dataGridView1.Rows.Count + 1, nv.NhanVien_TenDayDu, lnv.LoaiNhanVien_Ten, nv.NhanVien_Phai, nv.NhanVien_NgaySinh.ToString(), nv.NhanVien_DiaChi, nv.NhanVien_Email, nv.NhanVien_SoDienThoai, nv.NhanVien_Luong, nv.NhanVien_TenDangNhap);
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Tag = nv;
            }
            List<LoaiNhanVien> listLNV = ws.LoaiNhanVien_LayDanhSach();
            comboBox1.DataSource = listLNV;
            comboBox1.DisplayMember = "LoaiNhanVien_Ten";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn muốn xóa những nhân viên đã chọn?", "Xác nhận", MessageBoxButtons.YesNoCancel);
            if (dr == DialogResult.Yes)
            {
                foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
                {
                    DataGridViewRow row = dataGridView1.Rows[cell.RowIndex];

                    NhanVien nv = (NhanVien)row.Tag;

                    ws.NhanVien_Xoa(nv);

                    dataGridView1.Rows.RemoveAt(cell.RowIndex);
                }

                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    row.SetValues(row.Index + 1);
                }
            }
            else
            {
                return;
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if(isEditing)
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "JPG Files|*.jpg";
                open.Title = "Chọn hình ảnh để thay đổi";
                open.InitialDirectory = Environment.CurrentDirectory;
                if (open.ShowDialog() == DialogResult.OK)
                {
                    temp.NhanVien_HinhAnh = open.FileName;
                }

                Image itemp = Image.FromFile(temp.NhanVien_HinhAnh);
                Graphics g = pictureBox2.CreateGraphics();
                g.DrawImage(itemp, new Rectangle(0, 0, 110, 110), new Rectangle(0, 0, itemp.Width, itemp.Height), GraphicsUnit.Pixel);
                g.Dispose();
            }
        }
    }
}
